package com.ruoyi.web.controller.main;

import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.system.domain.InsuredPersons;
import com.ruoyi.system.domain.MedicalLnsuranceBureau;
import com.ruoyi.system.service.IInsuredPersonsService;
import com.ruoyi.system.service.IMedicalLnsuranceBureauService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.AuditDepartInfo;
import com.ruoyi.system.service.IAuditDepartInfoService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 审计处管理Controller
 * 
 * @author ruoyi
 * @date 2025-04-13
 */
@Api("审计处管理")
@RestController
@RequestMapping("/system/auditDepartInfo")
public class AuditDepartInfoController extends BaseController
{
    @Autowired
    private IAuditDepartInfoService auditDepartInfoService;

    @Autowired
    private IInsuredPersonsService insuredPersonsService;

    @Autowired
    private IMedicalLnsuranceBureauService medicalLnsuranceBureauService;

    /**
     * /system/auditDepartInfo/list
     */
    /**
     * 查询审计处管理列表
     */
    @ApiOperation("列表")
    @GetMapping("/list")
    public TableDataInfo list(AuditDepartInfo auditDepartInfo)
    {
        startPage();
        List<AuditDepartInfo> list = auditDepartInfoService.selectAuditDepartInfoList(auditDepartInfo);
        if(CollectionUtils.isNotEmpty(list)){
            list = list.stream().map(item -> {
                InsuredPersons insuredPersons = insuredPersonsService.selectInsuredPersonsById(item.getInsuredPersonId());
                if(ObjectUtils.isNotEmpty(insuredPersons)){
                    item.setInsuredPersons(insuredPersons);
                }
                return item;
            }).collect(Collectors.toList());
        }
        return getDataTable(list);
    }

    /**
     * /system/auditDepartInfo/notice
     */
    /**
     * 通知医保局
     * @param param
     * @return
     */
    @ApiOperation("通知医保局")
    @PostMapping("/notice")
    public AjaxResult notice(@RequestBody AuditDepartInfo param)
    {
        AuditDepartInfo auditDepartInfo = auditDepartInfoService.selectAuditDepartInfoById(param.getId());
        if(ObjectUtils.isNotEmpty(auditDepartInfo)){
            MedicalLnsuranceBureau bureau = new MedicalLnsuranceBureau();
            bureau.setIdentificationCard(auditDepartInfo.getIdentificationCard());
            bureau.setInsuredPersonId(auditDepartInfo.getInsuredPersonId());
            bureau.setMedicalInstitution(auditDepartInfo.getMedicalInstitution());
            bureau.setReimbursementAmount(auditDepartInfo.getReimbursementAmount());
            bureau.setNotificationTime(new Date());
            bureau.setPayMoney(auditDepartInfo.getPayMoney());
            bureau.setStatus(0L); //未读
            bureau.setVisitTime(auditDepartInfo.getVisitTime());
            medicalLnsuranceBureauService.insertMedicalLnsuranceBureau(bureau);

            auditDepartInfo.setStatus(1L);
            auditDepartInfoService.updateAuditDepartInfo(auditDepartInfo);
            return success("通知成功");
        }
        return error("通知失败");
    }



    /**
     * 获取审计处管理详细信息
     */
    @ApiOperation("获取审计处管理详细信息")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        AuditDepartInfo auditDepartInfo = auditDepartInfoService.selectAuditDepartInfoById(id);
        InsuredPersons insuredPersons = insuredPersonsService.selectInsuredPersonsById(auditDepartInfo.getInsuredPersonId());
        if(ObjectUtils.isNotEmpty(insuredPersons)){
            auditDepartInfo.setInsuredPersons(insuredPersons);
        }
        return success(auditDepartInfo);
    }

    /**
     * 新增审计处管理
     */
    @ApiOperation("新增审计处管理")
    @Log(title = "审计处管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody AuditDepartInfo auditDepartInfo)
    {
        return toAjax(auditDepartInfoService.insertAuditDepartInfo(auditDepartInfo));
    }

    /**
     * 修改审计处管理
     */
    @ApiOperation("修改审计处管理")
    @Log(title = "审计处管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody AuditDepartInfo auditDepartInfo)
    {
        return toAjax(auditDepartInfoService.updateAuditDepartInfo(auditDepartInfo));
    }

    /**
     * 删除审计处管理
     */
    @ApiOperation("删除审计处管理")
    @Log(title = "审计处管理", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(auditDepartInfoService.deleteAuditDepartInfoByIds(ids));
    }
}
